<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>更新学员</title>
    <script type="text/javascript" src="/js/jquery-1.11.1.js"></script>
    <script type="text/javascript" src="/js/bootstrap.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap.css"></link>
    <link rel="stylesheet" type="text/css" href="/css/style.css"></link>
    <style>
        label span{color: #ff0000; padding-right: 5px;}
    </style>
</head>

<body>
<h2>更新学员</h2>
<form id="myForm" class="form-horizontal">
    <!--springmvc的restful风格处理非get post方式请求必备参数-->
    <!--<input name="_method"  type="hidden" value="PUT"/>-->
    <div class="form-group">
        <label class="col-sm-1 control-label"><span></span>名字</label>
        <div class="col-sm-9">
            <input name="stuname"  type="text" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">学号</label>
        <div class="col-sm-9">
            <input  name="stuno" class="form-control" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label"><span></span>性别</label>
        <div class="col-sm-2">
            <select name="sex" class="form-control">
                <option value=-1>请选择性别</option>
                <option value=0>男</option>
                <option value=1>女</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label"><span></span>电话</label>
        <div class="col-sm-9">
            <input  name="phone" class="form-control" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">邮箱</label>
        <div class="col-sm-9">
            <input name="email" class="form-control" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">所属地</label>
        <div class="col-sm-9">
            <input name="registered" class="form-control" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">专业</label>
        <div class="col-sm-9">
            <input name="profession" class="form-control" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">政治身份</label>
        <div class="col-sm-2">
            <select name="politics" class="form-control">
                <option value="-1">请选择政治身份</option>
                <option value="群众">群众</option>
                <option value="团员">团员</option>
                <option value="党员">党员</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-1 control-label"></label>
        <div class="col-sm-9">
            <button id="btnUpdate" type="button" class="btn btn-sm btn-info">提&nbsp;交</button>
            <button type="reset"  class="btn btn-sm btn-warning">重&nbsp;置</button>
        </div>
    </div>
</form>
</body>
</html>
<script>
    String.prototype.GetValue= function(para) {
        let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?")+1).match(reg);
        if (r!=null) return unescape(r[2]); return null;
    }
    $(function () {
        //根据地址获取teamId,如果null则执行增加，否则执行更新
        let url = document.location.toString();//获取URL
        let stuid=url.GetValue("stuid");
        let pageNum=url.GetValue("pageNum");
        let pageSize=url.GetValue("pageSize");
        //alert(stuid);
        //根据id查询要更新的球队信息并回显到页面
        queryById(stuid);
        //提交按钮的单击事件--实现真正的更新
        $("#btnUpdate").click(function () {
            $.ajax({
                type: "POST",//这里必须写POST
                url: "/student/"+stuid,
                data: $("#myForm").serialize()+"&_method=PUT",
                dataType:"json",
                success: function(vo){
                    if(vo.code==200) {
                        alert("更新成功！")
                        window.location.href = "/pages/stu.html?pageNum="+pageNum+"&pageSize="+pageSize;
                    }else{
                        alert("更新失败！"+vo.msg);
                    }
                }
            });
        });
    });
    function queryById(id) {
        $.ajax({
            type: "GET",
            url: "/student/"+id, //RESTful风格的API定义
            data: "",
            success: function (vo) {
                let obj=vo;
                let stuname=obj.stuname;
                let stuno=obj.stuno;
                let sex=obj.sex;
                let phone=obj.phone;
                let email=obj.email;
                let registered=obj.registered;
                let profession=obj.profession;
                let politics=obj.politics;

                $("input[name='stuname']").val(stuname);
                $("input[name='stuno']").val(stuno);
                $("select[name='sex']").val(sex);
                $("input[name='phone']").val(phone);
                $("input[name='email']").val(email);
                $("input[name='registered']").val(registered);
                $("input[name='profession']").val(profession);
                $("select[name='politics']").val(politics);
            }
        });
    }

</script>